import React from 'react';
import { Modal, Form, Input, InputNumber } from 'antd';
import { email } from 'configs/rules';

const FormItem = Form.Item;

class AddModal extends React.Component {
  constructor (props) {
    super(props);
    this.state = {};
    this.onOK = this.onOK.bind(this);
  }

  onOK () {
    const { onOK, form, onCancel, record, getList } = this.props;
    form.validateFields((err, values) => {
      if (!err) {
        onOK({ ...values, id: record.id }).then(() => {
          onCancel();
          getList();
        });
      }
    });
  }

  render () {
    const { onCancel, form, loading, record = {} } = this.props;
    const { getFieldDecorator } = form;
    const modalOpts = {
      confirmLoading: loading,
      title: `${record.id ? '编辑' : '新增'}用户`,
      width: 500,
      visible: true,
      maskClosable: false,
      okText: '确定',
      cancelText: '取消',
      onOk: this.onOK,
      onCancel
    };
    const formItemLayout = {
      labelCol: {
        span: 4
      },
      wrapperCol: {
        span: 20
      }
    };
    return (
      <Modal {...modalOpts}>
        <Form>
          <FormItem
            label='用户名'
            {...formItemLayout}
          >
            {
              getFieldDecorator('username', {
                initialValue: record.username || '',
                rules: [{ required: true, message: '请填写用户名' }]
              })(
                <Input
                  maxLength={30}
                  placeholder='用户名'
                />
              )
            }
          </FormItem>
          {
            !record.id &&
            <FormItem
              label='密码'
              {...formItemLayout}
            >
              {
                getFieldDecorator('password', {
                  initialValue: '',
                  rules: [{ required: true, message: '请填写密码' }]
                })(
                  <Input
                    // disabled={!!record.id}
                    type='password'
                    maxLength={30}
                    placeholder='密码'
                  />
                )
              }
            </FormItem>
          }
          <FormItem
            label='邮箱'
            {...formItemLayout}
          >
            {
              getFieldDecorator('email', {
                initialValue: record.email || '',
                rules: [
                  { required: true, message: '请填写邮箱' },
                  email
                ]
              })(
                <Input
                  maxLength={30}
                  placeholder='邮箱'
                />
              )
            }
          </FormItem>
          <FormItem
            label='年龄'
            {...formItemLayout}
          >
            {
              getFieldDecorator('age', {
                initialValue: record.age || ''
              })(
                <InputNumber
                  min={0}
                  max={200}
                  precision={0}
                  style={{ width: '100%' }}
                  placeholder='年龄'
                />
              )
            }
          </FormItem>
        </Form>
      </Modal>
    );
  }
}

export default Form.create()(AddModal);
